<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Member Admin Controller
 *
 * @package		SimplyPost
 * @subpackage	Member
 * @category	Controller
 * @author		Pascal Kriete
 */

class Admin extends Administrator {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Admin()
	{
		parent::Administrator();
		$this->lang->load('member');
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * No real function - redirects
	 *
	 * @access	public
	 */
	function index()
	{
		redirect('admin');
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Add a new user
	 *
	 * @access	public
	 */
	function add_user()
	{
		set_title($this->lang->line('member_add_user_title'));
		$this->breadcrumb->from_path();
			
		$this->load->helper('form');
		$this->load->library('validation');

		$rules['username'] = "trim|required|xss_clean";
		$rules['email'] = "trim|required";
		$rules['password'] = "trim|requred|callback__verify";
		$this->validation->set_rules($rules);

		$fields['name'] = 'Name';
		$fields['email'] = "E-mail";
		$fields['password'] = "Password";
		$this->validation->set_fields($fields);

		$this->validation->set_error_delimiters('<p style="color:#900">', '</p>');

		if ($this->validation->run() == FALSE)
		{
			$this->parser->parse('member/admin/add');
		}
		else
		{
			$this->session->set_flashdata('msg', $this->lang->line('user_added'));
			redirect('home/');
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Add User Validation Callback
	 *
	 * @access	private
	 * @param	string	posted password
	 * @return	bool
	 */
	function _verify($data)
	{
		$username = $this->input->post('username');
		$email = $this->input->post('email');
		$password = secure_password($email, $data);

		$this->load->model('User_model', 'User');
		if($this->User->exists(array('username' => $username)) )
		{
			$this->validation->set_message('_verify', 'This username is already in use.');
			return FALSE;
		}
		else if( $this->User->exists(array('email' => $email)) )
		{
			$this->validation->set_message('_verify', 'This email address is already in use.');
			return FALSE;
		}
		else
		{
			$this->User->create($username, $email, $password);
			return TRUE;
		}
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Delete a user
	 *
	 * @access	public
	 * @param	integer	user id
	 */
	function delete($id)
	{
		if ($id == $this->_user['id'])
		{
			$this->session->set_flashdata('msg', $this->lang->load('delete_self_deny'));
			redirect('member/profile/view/'.$id);
		}
		else if ($id == 1)
		{
			$this->session->set_flashdata('msg', $this->lang->load('delete_admin_deny'));
			redirect('member/profile/view/'.$id);
		}
		else {
			$this->load->model('User_model', 'User');
			$this->User->delete($id);
			$this->session->set_flashdata('msg', $this->lang->load('user_deleted'));
			redirect('home');
		}
	}

}